package com.aaa.aspect;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import com.aaa.entity.systemmanage.User;
import lombok.extern.java.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author ：xiaoxuan
 * @date ：Created in 2022/9/6 15:58
 * @description：切面类，专门对添加方法进行增强处理
 * @modified By：
 * @version:
 */
@Component
@Aspect
@EnableAspectJAutoProxy
@Log
public class MyInsertUpdateAspect {
    @Pointcut("@annotation(com.aaa.aspect.InsertAnn)")
    public void xxx(){}
    @Pointcut("@annotation(com.aaa.aspect.UpdateAnn)")
    public void yyy(){}

    @Before("xxx()")
    public void insertEnhance(JoinPoint joinPoint){
        //增强处理，将方法的参数获取出来，补充两个属性的值，一个是createTime，一个是createBy
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        try {
            if(ObjectUtil.isNotEmpty(args)){
                //如果不为空，可以获取第一个参数
                Object argFirst = args[0];
                //对上面的参数进行增强处理,让argFirst对象进行方法setCreateTime的自动执行
                ReflectUtil.invoke(argFirst,"setCreateTime",new Date());
                //补充创建人
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = requestAttributes.getRequest();
                User user = (User) request.getSession().getAttribute("user");
                String username = user.getUsername();
                ReflectUtil.invoke(argFirst,"setCreateBy",username);
            }
        }catch ( Exception e){
            log.info("实体类中没有create相关字段");
        }

    }
    @Before("yyy()")
    public void updateEnhance(JoinPoint joinPoint){
        //增强处理，将方法的参数获取出来，补充两个属性的值，一个是updateTime，一个是updateBy
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        try {
            if(ObjectUtil.isNotEmpty(args)){
                //如果不为空，可以获取第一个参数
                Object argFirst = args[0];
                //对上面的参数进行增强处理,让argFirst对象进行方法setUpdateTime的自动执行
                ReflectUtil.invoke(argFirst,"setUpdateTime",new Date());
                //补充修改人
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = requestAttributes.getRequest();
                User user = (User) request.getSession().getAttribute("user");
                String username = user.getUsername();
                ReflectUtil.invoke(argFirst,"setUpdateBy",username);
            }
        }catch ( Exception e){
            log.info("实体类中没有update相关字段");
        }

    }
}
